//
//  ZFPageControlConfig.m
//  components
//
//  Created by linzaifei on 2021/9/2.
//

#import "ZFPageControlView.h"
#define ZFWeakSelf __weak typeof(self) weakSelf = self
#define ZFStrongSelf __strong typeof(weakSelf) strongSelf = weakSelf;


@interface ZFPageControlView()

@property(nonatomic,strong) UIPageControl *pageControl;
@property(nonatomic,strong) ZFPageControl *zfpageControl;
@end

@implementation ZFPageControlView

- (UIPageControl *)pageControl{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.userInteractionEnabled = NO;
        _pageControl.hidden = YES;
    }
    return _pageControl;
}
- (ZFPageControl *)zfpageControl{
    if (_zfpageControl == nil) {
        _zfpageControl = [ZFPageControl new];
        _zfpageControl.userInteractionEnabled = NO;
        _zfpageControl.hidden = YES;
    }
    return _zfpageControl;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        _style = ZFHorizontalMenuViewPageControlStyleClassic;
        _aliment = ZFHorizontalMenuViewPageControlAlimentCenter;
        _defaultConfig = [ZFPageControlConfig new];
        [self initZFPageControlView];
    }
    return self;
}

-(void)initZFPageControlView{
    [self addSubview:self.pageControl];
    [self addSubview:self.zfpageControl];
    self.translatesAutoresizingMaskIntoConstraints = NO;

    [self addObserver:self forKeyPath:@"currentPage" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"numberOfPages" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"defaultConfig.size" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"defaultConfig.edgeInsets" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"defaultConfig.space" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"defaultConfig.currentPageIndicatorTintColor" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"defaultConfig.pageIndicatorTintColor" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if([keyPath isEqualToString:@"currentPage"]){
        if (_style == ZFHorizontalMenuViewPageControlStyleClassic){
            self.pageControl.currentPage = [change[@"new"] integerValue];
        }else if (_style == ZFHorizontalMenuViewPageControlStyleAnimated){
            self.zfpageControl.currentPage = [change[@"new"] integerValue];
        }
    }else if([keyPath isEqualToString:@"numberOfPages"]){
        if (_style == ZFHorizontalMenuViewPageControlStyleClassic){
            self.pageControl.numberOfPages = [change[@"new"] integerValue];
        }else if (_style == ZFHorizontalMenuViewPageControlStyleAnimated){
            self.zfpageControl.numberOfPages = [change[@"new"] integerValue];
        }
    }else if([keyPath isEqualToString:@"defaultConfig.size"] || [keyPath isEqualToString:@"defaultConfig.edgeInsets"]||[keyPath isEqualToString:@"defaultConfig.space"] ||[keyPath isEqualToString:@"defaultConfig.currentPageIndicatorTintColor"]||[keyPath isEqualToString:@"defaultConfig.pageIndicatorTintColor"]){
        if (_style == ZFHorizontalMenuViewPageControlStyleClassic){
            self.pageControl.currentPageIndicatorTintColor = self.defaultConfig.currentPageIndicatorTintColor;
            self.pageControl.pageIndicatorTintColor = self.defaultConfig.pageIndicatorTintColor;
        }else if (_style == ZFHorizontalMenuViewPageControlStyleAnimated){
            self.zfpageControl.defaultConfig.currentPageIndicatorTintColor = self.defaultConfig.currentPageIndicatorTintColor;
            self.zfpageControl.defaultConfig.pageIndicatorTintColor = self.defaultConfig.pageIndicatorTintColor;
            self.zfpageControl.defaultConfig.space = self.defaultConfig.space;
            self.zfpageControl.defaultConfig.edgeInsets = self.defaultConfig.edgeInsets;
            self.zfpageControl.defaultConfig.size = self.defaultConfig.size;
        }
    }
    
}


- (void)layoutSubviews {
    [super layoutSubviews];

    if (self.style == ZFHorizontalMenuViewPageControlStyleNone){
        return;
    }
    self.pageControl.hidden = self.style == ZFHorizontalMenuViewPageControlStyleAnimated;
    self.zfpageControl.hidden = self.style == ZFHorizontalMenuViewPageControlStyleClassic;
    if (self.style == ZFHorizontalMenuViewPageControlStyleClassic){
        if (self.aliment == ZFHorizontalMenuViewPageControlAlimentCenter) {
            [self.pageControl mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(@(self.defaultConfig.edgeInsets.top));
                make.bottom.equalTo(@(-self.defaultConfig.edgeInsets.bottom));
                make.centerX.equalTo(self.mas_centerX);
            }];
        }else if (self.aliment == ZFHorizontalMenuViewPageControlAlimentRight){
            [self.pageControl mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(@(self.defaultConfig.edgeInsets.top));
                make.bottom.equalTo(@(-self.defaultConfig.edgeInsets.bottom));
                make.right.equalTo(@(-self.defaultConfig.edgeInsets.right));
            }];
        }
        
    }else if (self.style == ZFHorizontalMenuViewPageControlStyleAnimated) {
 
        if (self.aliment == ZFHorizontalMenuViewPageControlAlimentCenter) {
            [self.zfpageControl mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(self);
                make.centerX.equalTo(self.mas_centerX);
            }];
        }else if (self.aliment == ZFHorizontalMenuViewPageControlAlimentRight){
            [self.zfpageControl mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(self);
                make.right.equalTo(self);
            }];
        }
        
    }
    
}


-(void)dealloc{
    [self removeObserver:self forKeyPath:@"numberOfPages"];
    [self removeObserver:self forKeyPath:@"currentPage"];
    [self removeObserver:self forKeyPath:@"defaultConfig.size"];
    [self removeObserver:self forKeyPath:@"defaultConfig.edgeInsets"];
    [self removeObserver:self forKeyPath:@"defaultConfig.space"];
    [self removeObserver:self forKeyPath:@"defaultConfig.currentPageIndicatorTintColor"];
    [self removeObserver:self forKeyPath:@"defaultConfig.pageIndicatorTintColor"];
}
@end

@interface ZFPageControl()
@property(nonatomic,copy)NSMutableArray <UIView *>*items;
@property(nonatomic,copy)NSMutableArray <MASCompositeConstraint *>*constraints;
@end
@implementation ZFPageControl

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initZFPageControl];
    }
    return self;
}
-(void)initZFPageControl{
    self.currentPage = 0;
    _constraints = [NSMutableArray array];
    _defaultConfig = [ZFPageControlConfig new];
    
    [self addObserver:self forKeyPath:@"currentPage" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"numberOfPages" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"defaultConfig.size" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"defaultConfig.edgeInsets" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"defaultConfig.space" options:NSKeyValueObservingOptionNew context:nil];
    
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if([keyPath isEqualToString:@"currentPage"]){
        [self changeView:[change[@"new"] integerValue]];
    }else if([keyPath isEqualToString:@"numberOfPages"]){
        [self layoutView];
    }else if([keyPath isEqualToString:@"defaultConfig.size"] || [keyPath isEqualToString:@"defaultConfig.edgeInsets"]||[keyPath isEqualToString:@"defaultConfig.space"]){
        [self layoutConstraints];
    }
}
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"currentPage"];
    [self removeObserver:self forKeyPath:@"numberOfPages"];
    [self removeObserver:self forKeyPath:@"defaultConfig.size"];
    [self removeObserver:self forKeyPath:@"defaultConfig.edgeInsets"];
    [self removeObserver:self forKeyPath:@"defaultConfig.space"];
}

#pragma mark --- 切换
-(void)changeView:(NSInteger )index{
    if(self.items.count==0)return;
    ZFWeakSelf;
    [self.items enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ZFStrongSelf;
        obj.backgroundColor = index==idx? strongSelf.defaultConfig.currentPageIndicatorTintColor:strongSelf.defaultConfig.pageIndicatorTintColor;
    }];
}

#pragma mark --- 创建视图
-(void)layoutView{
    [self.items removeAllObjects];
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (int i=0; i<self.numberOfPages; i++) {
        UIView *dotView = [UIView new];
        dotView.backgroundColor = self.defaultConfig.pageIndicatorTintColor;
        [self addSubview:dotView];
        dotView.layer.cornerRadius = self.defaultConfig.size.height / 2.0;
        dotView.layer.masksToBounds = YES;
        [self.items addObject:dotView];
    }
    [self layoutConstraints];
}


-(void)layoutConstraints{
    for (MASConstraint * constraint in self.constraints) {
        [constraint uninstall];
    }
    [self.constraints removeAllObjects];
    if(self.items.count == 0){
        return;
    }
    if(self.items.count == 1){
       [self.constraints addObjectsFromArray: [self.items.firstObject mas_makeConstraints:^(MASConstraintMaker *make) {
           make.edges.equalTo(self).with.insets(self.defaultConfig.edgeInsets);
           make.size.equalTo(@(self.defaultConfig.size));
       }]];
        return;
    }

   [self.constraints addObjectsFromArray:[self.items mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self).equalTo(@(self.defaultConfig.edgeInsets.top));
       make.bottom.equalTo(self).equalTo(@(-self.defaultConfig.edgeInsets.bottom));
       make.size.equalTo(@(self.defaultConfig.size));
   }]];
    
   [self.items mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:self.defaultConfig.space leadSpacing:0 tailSpacing:0];
}

#pragma mark --- getter
- (NSMutableArray<UIView *> *)items{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}
@end


@implementation ZFPageControlConfig

- (instancetype)init
{
    self = [super init];
    if (self) {
        _size = CGSizeMake(14, 4);
        _space = 5;
        _edgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
        _currentPageIndicatorTintColor = [UIColor orangeColor];
        _pageIndicatorTintColor = [UIColor grayColor];
    }
    return self;
}

@end
